package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.pojo.City;
import com.qf.pojo.User;
import com.qf.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class CityController {

    @Autowired
    private CityService cityService;

    //    用pid取名字。联
    @RequestMapping (value = "/city" , method =  RequestMethod.GET)
    public  List<City> selectOne(int pid ) {
        List<City> cities = cityService.selectOne(pid);
        return cities;
    }


//    用id取名字
    @GetMapping ("/selectById{id}")
    public City  selectById(@PathVariable("id")int  id){
        City city = cityService.selectById(id);
        return city;
    }


    @GetMapping ("/List")
    public  PageInfo<City> select(HttpServletRequest req, HttpServletResponse resp) {
        String page = req.getParameter("page");
        int p=page==null?1:Integer.valueOf(page);
        PageInfo<City> alls = cityService.select(p,3);
        return alls;
    }


    @PostMapping  ("/add")
    public City add(@RequestBody City city){
        City add = cityService.add(city);
        return add;
    }

    @DeleteMapping  ("/del{id}")
    public int  delete(@PathVariable("id") int id){
        int delete = cityService.delete(id);
        return delete;

    }


    @PutMapping("/update")
    public City update(@RequestBody City city){
//        System.out.println("11"+city);
        City update = cityService.update(city);
        return update;
    }

}
